package com.guo.hui.service;

import com.alibaba.fastjson.JSONObject;
import com.guo.hui.dao.MyUserMapper;
import com.guo.hui.entity.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserService
 * @Description TODO
 * @Author zhengguohui
 * @Date 2018/8/31 16:33
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private MyUserMapper userMapper;

    @Override
    public List<MyUser> getUserList(JSONObject object) {
        Map<String, Object> map = new HashMap<>();
        Integer page = object.getInteger("page");
        Integer pageSize = object.getInteger("pageSize");
        if (page != null && pageSize != null) {
            map.put("page", (page - 1) * pageSize);
            map.put("pageSize", pageSize);
        }
        return userMapper.getMyUserList(map);
    }

    @Override
    public MyUser getMyUser(JSONObject object) {
        String username = object.getString("username");
        String password = object.getString("password");
//        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
//            MyUser myUser = userMapper.findMyUser(username);
//            return myUser;
//        }
        return null;
    }

    @Override
    public void addUser(JSONObject object) {
        MyUser myUser = object.toJavaObject(MyUser.class);
        userMapper.insert(myUser);

    }

    @Override
    public void editUser(JSONObject object) {
        MyUser myUser = object.toJavaObject(MyUser.class);
        userMapper.updateByPrimaryKey(myUser);
    }

    @Override
    public void deleteUser(JSONObject object) {
        Integer id = object.getInteger("id");
        userMapper.deleteByPrimaryKey(id);
    }
}
